<!doctype html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/main.css" rel="stylesheet" />
		<script src="js/mui.min.js"></script>
		<style type="text/css">
			#bcid{
					width: 100%;
					height: 100%;
					position: absolute;
					background: #000000;
				}
		</style>
	</head>
		
	<body>
		<header class="mui-bar mui-bar-nav">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left">微信</a>
		    <h1 class="mui-title">二维码/条码</h1>
		    <a class=" mui-icon mui-pull-right" id=turnPhoto>相册</a>
		    </header>
		
		<!--盛放扫描控件的div--> 
		<div id="bcid">
		</div>
		<span  id="turnTheLight" style="float: right;color:white;position: absolute;top: 50%;left: 50%; ">打开闪光灯</span>
		
	    <nav class="mui-bar mui-bar-tab">
	        <a class="mui-tab-item mui-active">
	            <span class="mui-icon mui-icon-home"></span>
	            <span class="mui-tab-label">扫码</span>
	        </a>
	        <a class="mui-tab-item">
	            <span class="mui-icon mui-icon-phone"></span>
	            <span class="mui-tab-label">封面</span>
	        </a>
	        <a class="mui-tab-item">
	            <span class="mui-icon mui-icon-email"></span>
	            <span class="mui-tab-label">街景</span>
	        </a>
	    </nav>
	</body>
	<script type="text/javascript">
		var scan = null;//扫描对象
		/*var height = window.innerHeight + 'px';//获取页面实际高度
		var width = window.innerWidth + 'px';
		document.getElementById("bcid").style.height= height;
		document.getElementById("bcid").style.width= width;*/
	
		mui.plusReady(function () {
	      	mui.init();
	      	//开始扫码
	  		startRecognize();
	  		//打开相册
	  		document.getElementById("turnPhoto").addEventListener('tap',function(){
	  			scanPicture();
	  		})
	   	});
		function startRecognize(){
		   	try{
			  	var filter;//扫码格式 空为全类型
			 	//自定义的扫描控件样式
			 	var styles = {frameColor: "#29E52C",scanbarColor: "#29E52C"}
				//扫描控件构造
				scan = new plus.barcode.Barcode('bcid',filter,styles);
				scan.onmarked = onmarked; 
				scan.onerror = onerror;
				scan.start();
				//打开关闭闪光灯处理
				var flag = false;
				document.getElementById("turnTheLight").addEventListener('tap',function(){
				   if(flag == false){
				      scan.setFlash(true);
				      flag = true;
				   }else{
				     scan.setFlash(false);
				     flag = false;
				   }
				});
		  	}catch(e){
				alert("出现错误啦:\n"+e);
		    }
		};
		function onerror(e){
				alert(e);
		};
		function onmarked( type, result ) {
			var text = '';
			switch(type){
				case plus.barcode.QR:
				text = 'QR: ';
				break;
				case plus.barcode.EAN13:
				text = 'EAN13: ';
				break;
				case plus.barcode.EAN8:
				text = 'EAN8: ';
				break;
			}
			alert( text + " : "+ result );
		};	
			    
		// 从相册中选择二维码图片 
		function scanPicture() {
		    plus.gallery.pick(function(path){
			    plus.barcode.scan(path,onmarked,function(error){
					plus.nativeUI.alert( "无法识别此图片" );
				});
		    },function(err){
		        plus.nativeUI.alert("Failed: "+err.message);
		    });
		}	    
	
	</script>
</html>